class Solution {
public:
    int prev;
    int _color;
    int m, n;
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        if (image[sr][sc] == color) return image;
        prev = image[sr][sc];
        image[sr][sc] = _color = color;
        m = image.size(), n = image[0].size();
        dfs(image, sr, sc);
        return image;
    }
    int row[4] = { -1,1,0,0 };
    int col[4] = { 0,0,-1,1 };
    void dfs(vector<vector<int>>& image, int r, int c)
    {
        for (int i = 0;i < 4;i++)
        {
            int x = r + row[i], y = c + col[i];
            if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev)
            {
                image[x][y] = _color;
                dfs(image, x, y);

            }
        }

    }
};